<?php

$opr = !isset($_SESSION["SESS_PROSES"]) ? $_SESSION["SESS_PROSES"] = 'proses1' : $_SESSION["SESS_PROSES"];
$sess_id = session_id();

if (isset($_GET["kode"])) {
    $kode = !isset($_GET["kode"]) ? 0 : $_GET["kode"];
    echo '<iframe src = "hasil.php?kode=' . $kode . '" width="700" height="730" frameborder="0" scrolling="yes"></iframe >';

// home
} elseif ($opr == 'proses1') {
    load_jquery_update();
    load_js_validate();
    echo "\n <div id=\"ki_detail\">";
    echo "\n <div id=\"kid_atas\">";
    echo "\n <h2>Keranjang Belanja</h2>";
    $sql = "SELECT b.produk_nama as produk_nama, b.produk_gambar as produk_gambar, b.produk_berat as produk_berat, a.trt_ukuran as ukuran, b.produk_diskon as diskon, a.trt_jumlah as jumlah, a.trt_harga as harga, a.trt_id as id, a.trt_berat as berat " .
            " FROM tr_t a LEFT JOIN produks b on (a.trt_produk_id=b.produk_id) WHERE a.trt_session='$sess_id' ORDER BY a.trt_waktu";
    $hasil = mysql_query($sql);
    $ketemu = mysql_num_rows($hasil);

    $total_berat = 0;
    $total_jumlah = 0;
    $total_harga = 0;

    if ($ketemu < 1) {
        echo "<p style=\"text-align:center;\">Keranjang Belanja Masih Kosong</p>";
    } else {
        $no = 1;
        echo "\n <table>";
        echo "\n  <tr><th>No</th><th>Nama Produk</th><th>Gambar</th><th>Ukuran</th><th>Berat (gram)</th><th>Diskon</th><th>Jumlah</th><th>Harga</th><th></th></tr>";
        while ($row = mysql_fetch_array($hasil)) {
            echo "<tr>";
            echo "<td>$no</td>";
            echo "<td>" . $row["produk_nama"] . "</td>";
            echo "<td align='center'><img src=" . URLHOST . "media/foto_produk/small_" . $row["produk_gambar"] . " height='70px' /></td>";
            echo "<td align='center'>" . strtoupper($row["ukuran"]) . "</td>";
            echo "<td align='right'>" . $row["berat"] . "</td>";
            echo "<td align='right'>" . $row["diskon"] . " %</td>";
            echo "<td align='center'>" . $row["jumlah"] . "</td>";
            echo "<td align='right'>Rp " . format_rupiah($row["harga"]) . "</td>";
            echo "<td align='center'><a href='#' id=\"edit" . $row["id"] . "\">edit</a> <a href=\"" . URLHOST . "transaksi/perintah.php?opr=hapus&amp;id=" . $row["id"] . "\">delete</a>";
            echo "</div>";
            echo "</td>";
            echo "</tr>";
            echo "<tr><td colspan='9' align='right'>";
            echo "<div id=\"formbox" . $row["id"] . "\" style=\"display:none\">";
            echo "<form id=\"formbox" . $row["id"] . "\" method=\"post\" action=\"" . URLHOST . "transaksi/perintah.php?opr=update&amp;id=" . $row["id"] . "\"  name=\"form" . $row["id"] . "\">";
            echo "Ukuran : " . get_ukuran_select("in_size", $row["ukuran"]);
            echo " Jumlah : <input type=\"text\" name=\"in_jumlah\" size=\"3\" maxlength=\"3\" value=\"" . $row["jumlah"] . "\" onkeypress=\"return isNumberKey(event)\" />";
            echo "<input type=\"submit\" value=\" Save \" class=\"save\" />or";
            echo "<input type=\"button\" value=\" Cancel \" class=\"cancel\" />";
            echo "</form>";
            echo "\n </div>";
            echo "</td></tr>";
            $no++;
            $total_jumlah += $row["jumlah"];
            $total_harga +=$row["harga"];
            $total_berat +=$row["berat"];
        }
        echo "<tr><td colspan='4' align='center'>Total</td><td align='right'>$total_berat</td><td></td><td align='center'>$total_jumlah</td><td align='right'>Rp " . format_rupiah($total_harga) . "</td><td></td></tr>";
        echo "</table>";
    }
    echo "\n</div>";
    echo "\n <div id=\"kid_bawah\"><a href=\"" . URLPUBLICINDEX . "?opr=home\">Belanja Lagi</a> ";
    if ($ketemu > 0) {
        echo "<a href=\"" . URLHOST . "transaksi/perintah.php?opr=checkout&amp;id=$sess_id\">Checkout Now</a>";
    }
    echo "\n </div>";
    echo "\n </div>";

// proses2    
} elseif ($opr == 'proses2') {
    $trx_id = $_SESSION["SESS_TRX"];
    echo "\n <div id=\"ki_detail\">";
    echo "\n <div id=\"kid_atas\">";
    echo "\n <h2>Checkout</h2>";
    $sql = "SELECT b.produk_nama as produk_nama, b.produk_gambar as produk_gambar, b.produk_berat as produk_berat, a.trt_ukuran as ukuran, b.produk_diskon as diskon, a.trt_jumlah as jumlah, a.trt_harga as harga, a.trt_id as id, a.trt_berat as berat " .
            " FROM tr_t a LEFT JOIN produks b on (a.trt_produk_id=b.produk_id) WHERE a.trt_session='$sess_id' ORDER BY a.trt_waktu";
    $hasil = mysql_query($sql);
    $ketemu = mysql_num_rows($hasil);

    $total_berat = 0;
    $total_jumlah = 0;
    $total_harga = 0;

    if ($ketemu < 1) {
        echo "<p style=\"text-align:center;\">Keranjang Belanja Masih Kosong</p>";
        $_SESSION["SESS_PROSES"] = null;
        $_SESSION["SESS_TRX"] = null;
        lempar_ke_home();
    } else {
        $no = 1;
        echo "\n <table>";
        echo "\n  <tr><th>No</th><th>Nama Produk</th><th>Ukuran</th><th>Berat (gram)</th><th>Diskon</th><th>Jumlah</th><th>Harga</th></tr>";
        while ($row = mysql_fetch_array($hasil)) {
            echo "<tr>";
            echo "<td>$no</td>";
            echo "<td>" . $row["produk_nama"] . "</td>";
            echo "<td align='center'>" . strtoupper($row["ukuran"]) . "</td>";
            echo "<td align='right'>" . $row["berat"] . "</td>";
            echo "<td align='right'>" . $row["diskon"] . " %</td>";
            echo "<td align='center'>" . $row["jumlah"] . "</td>";
            echo "<td align='right'>Rp " . format_rupiah($row["harga"]) . "</td>";
            echo "</tr>";
            $no++;
            $total_jumlah += $row["jumlah"];
            $total_harga +=$row["harga"];
            $total_berat +=$row["berat"];
        }
        $_SESSION["SESS_BERAT"] = $total_berat;
        $trx_angkaunik = get_field_table_from_id($trx_id, 'trx_angkaunik', 'tr_x', 'trx_id');

        echo "<tr><td colspan='3' align='right'>Total</td><td align='right'>$total_berat</td><td></td><td align='center'>$total_jumlah</td><td align='right'>Rp " . format_rupiah($total_harga) . "</td></tr>";
        echo "<tr><td colspan='6' align='right'>Kode Unik Transfer</td><td align='right'>$trx_angkaunik</td></tr>";
        //echo "<tr><td colspan='6' align='right'>Jasa Pengiriman</td><td align='right'>$trx_angkaunik</td></tr>";
        echo "<tr><td colspan='6' align='right'>Total Belanja</td><td align='right'>Rp " . format_rupiah($total_harga + $trx_angkaunik) . "</td></tr>";
        echo "</table>";
    }
    echo "\n</div>";
    load_js_validate_email();
    echo "\n <div id=\"kid_tengah\">";
    echo "\n <h2>Alamat Pengiriman</h2>";
    echo "\n  <form id=\"frm\" method=\"post\" action=\"" . URLHOST . "transaksi/perintah.php\" onSubmit=\"return validate(this.in_nama.value, this.in_email.value, this.in_notelp.value, this.in_alamat.value, this.in_ongkir.value);\">";
    echo "\n   <input type=\"hidden\" name=\"opr\" value=\"simpan\" />";
    echo "\n   <label>Nama</label><input type=\"text\" name=\"in_nama\" id=\"in_nama\" /><br />";
    echo "\n   <label>Email</label><input type=\"text\" name=\"in_email\" id=\"in_email\" /><br />";
    echo "\n   <label>Telp/Hp</label><input type=\"text\" name=\"in_notelp\" id=\"in_notelp\" onkeypress=\"return isNumberKey(event)\" /><br />";
    echo "\n   <label>Alamat</label><textarea name=\"in_alamat\" id=\"in_alamat\"></textarea><br />";
    echo "\n   <label>Kota</label>" . get_ongkir_select('in_ongkir') . "<br />";
    echo "\n   <div id=\"kid_bawah\">";
    echo "\n    <input type=\"submit\" name=\"simpan\" value=\"Simpan\" />";
    echo "\n  </form>";
    echo "\n </div>";
    echo "\n </div>";
    echo "\n </div>";
    //echo "proses2" . $trx_id;
// proses2    
} elseif ($opr == 'proses3') {
    $trx_id = $_SESSION["SESS_TRX"];
    echo "\n <div id=\"ki_detail\">";
    echo "\n <div id=\"kid_atas\">";
    echo "\n <h2>Transaksi Final</h2>";
    $sql = "SELECT b.produk_nama as produk_nama, b.produk_gambar as produk_gambar, b.produk_berat as produk_berat, a.trt_ukuran as ukuran, b.produk_diskon as diskon, a.trt_jumlah as jumlah, a.trt_harga as harga, a.trt_id as id, a.trt_berat as berat " .
            " FROM tr_t a LEFT JOIN produks b on (a.trt_produk_id=b.produk_id) WHERE a.trt_session='$sess_id' ORDER BY a.trt_waktu";
    $hasil = mysql_query($sql);
    $ketemu = mysql_num_rows($hasil);

    $total_berat = 0;
    $total_jumlah = 0;
    $total_harga = 0;

    if ($ketemu < 1) {
        echo "<p style=\"text-align:center;\">Keranjang Belanja Masih Kosong</p>";
        $_SESSION["SESS_PROSES"] = null;
        $_SESSION["SESS_TRX"] = null;
        lempar_ke_home();
    } else {
        $no = 1;
        echo "\n <table>";
        echo "\n  <tr><th>No</th><th>Nama Produk</th><th>Ukuran</th><th>Berat (gram)</th><th>Diskon</th><th>Jumlah</th><th>Harga</th></tr>";
        while ($row = mysql_fetch_array($hasil)) {
            echo "<tr>";
            echo "<td>$no</td>";
            echo "<td>" . $row["produk_nama"] . "</td>";
            echo "<td align='center'>" . strtoupper($row["ukuran"]) . "</td>";
            echo "<td align='right'>" . $row["berat"] . "</td>";
            echo "<td align='right'>" . $row["diskon"] . " %</td>";
            echo "<td align='center'>" . $row["jumlah"] . "</td>";
            echo "<td align='right'>Rp " . format_rupiah($row["harga"]) . "</td>";
            echo "</tr>";
            $no++;
            $total_jumlah += $row["jumlah"];
            $total_harga +=$row["harga"];
            $total_berat +=$row["berat"];
        }

        $trx_id = $_SESSION["SESS_TRX"];
        $sql_trx = "SELECT trx_id, trx_kodeunik, trx_angkaunik, trx_total, trx_ongkir, 
                    trx_nama, trx_alamat, trx_notelp, trx_email, trx_ongkir_id FROM tr_x
                    WHERE trx_id='$trx_id' and trx_session='$sess_id' ";
        $hasil_trx = mysql_query($sql_trx);
        $baris_trx = mysql_num_rows($hasil_trx);

        if ($baris_trx == 1) {
            $trx_kodeunik = mysql_result($hasil_trx, "0", "trx_kodeunik");
            $trx_angkaunik = mysql_result($hasil_trx, "0", "trx_angkaunik");
            //$trx_total = mysql_result($hasil_trx, "0", "trx_total");
            $trx_ongkir = mysql_result($hasil_trx, "0", "trx_ongkir");
            $trx_nama = mysql_result($hasil_trx, "0", "trx_nama");
            $trx_alamat = mysql_result($hasil_trx, "0", "trx_alamat");
            $trx_notelp = mysql_result($hasil_trx, "0", "trx_notelp");
            $trx_email = mysql_result($hasil_trx, "0", "trx_email");
            $trx_ongkir_id = mysql_result($hasil_trx, "0", "trx_ongkir_id");
            $trx_grand_total = $total_harga + $trx_angkaunik + $trx_ongkir;
            $trx_kota = get_ongkir_kota_from_id($trx_ongkir_id);
            $_SESSION["SESS_TOTAL"] = $trx_grand_total;
        }

        echo "<tr><td colspan='3' align='right'>Total</td><td align='right'>$total_berat</td><td></td><td align='center'>$total_jumlah</td><td align='right'>Rp " . format_rupiah($total_harga) . "</td></tr>";
        echo "<tr><td colspan='6' align='right'>Kode Unik Transfer</td><td align='right'>$trx_angkaunik</td></tr>";
        echo "<tr><td colspan='6' align='right'>Jasa Pengiriman</td><td align='right'>" . format_rupiah($trx_ongkir) . "</td></tr>";
        echo "<tr><td colspan='6' align='right'>Total Keseluruhan</td><td align='right'>Rp " . format_rupiah($trx_grand_total) . "</td></tr>";
        echo "<tr><td colspan='7'></td></tr>";
        echo "<tr><td colspan='7'>Alamat Pengiriman (Kode transaksi: $trx_kodeunik)</td></tr>";
        echo "<tr><td colspan='2'>Nama</td><td colspan='5'>$trx_nama</td></tr>";
        echo "<tr><td colspan='2'>Email</td><td colspan='5'>$trx_email</td></tr>";
        echo "<tr><td colspan='2'>Telp/Hp</td><td colspan='5'>$trx_notelp</td></tr>";
        echo "<tr><td colspan='2'>Alamat</td><td colspan='5'>$trx_alamat</td></tr>";
        echo "<tr><td colspan='2'>Kota</td><td colspan='5'>$trx_kota</td></tr>";

        echo "</table>";
    }
    echo "\n  </div>";

    echo "\n  <div id=\"kid_bawah\">";
    echo "\n   <a href=\"" . URLHOST . "transaksi/perintah.php?opr=hapus-transaksi&amp;id=$sess_id\">Hapus Transaksi</a>";
    echo "\n   <a href=\"" . URLHOST . "transaksi/perintah.php?opr=finish&amp;id=$sess_id\">Selesai Transaksi</a>";
    echo "\n  </div>";
    echo "\n </div>";

    //echo "proses3";
// tidak semua
} else {
    lempar_ke_home();
}
?>
